'Updates the AME form with Staff and Access changes
Option Strict Off
imports System
imports PHSRAG.Insight21
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports PHSRAG.Insight21.FormsManagement
imports System.Collections

module Script
    sub Main()
		dim overallStaffChangesQuestion as string = nothing
		dim addStaffQuestion as string = nothing
		dim removeStaffQuestion as string = nothing
		dim addedStaffQuestion as string = nothing
		dim removedStaffQuestion as string = nothing
		dim addStaffAnswerValue as string = nothing
		dim removeStaffAnswerValue as string = nothing
		
		
		if (theProtocol.ModuleType = Humans.Constants.ModuleType.Animals) then
			overallStaffChangesQuestion = "TopChoice"
			addStaffQuestion = "ComStaff"
			addStaffAnswerValue = "STAFF"
			addedStaffQuestion = "AddStaff"
			removeStaffQuestion = "ComUpStaff"
			removeStaffAnswerValue = "DEL"
			removedStaffQuestion = "DelStaff"
		else
			overallStaffChangesQuestion = "AmendStaff"
			addStaffQuestion = "AddStaff"
			addStaffAnswerValue = "Add"
			addedStaffQuestion = "PersonalEx"
			removeStaffQuestion = "DeleteStaff"
			removeStaffAnswerValue = "Delete"
			removedStaffQuestion = "DeleteStaffName"
		end if

		dim amendStaffAnswers as ArrayList = questAnswer.GetAnswers(overallStaffChangesQuestion)
		dim amendStaffAnswer as Answer = amendStaffAnswers(0)
		if (amendStaffAnswer.ChildAnswers is nothing) then
			amendStaffAnswer.ChildAnswers = new ArrayList()
		end if

		if ( not isStaffBeingAdded) then
			'if person being removed is added by the current process, then remove the name from the Added Staff List
			if (staffPerson.AddedProtocolProcessId <> currentProcess.Id) then
				'add to removed staff list
				dim delStaffAnswers as ArrayList = questAnswer.GetAnswers(removeStaffQuestion)
				dim delStaffAnswer as Answer = nothing
				if (delStaffAnswers.Count > 0) then
					delStaffAnswer = delStaffAnswers(0)
					if (delStaffAnswer.ChildAnswers is nothing) then
						delStaffAnswer.ChildAnswers = new ArrayList()
					end if
				else
					delStaffAnswer = new Answer(cachePolicy)
					delStaffAnswer.Name = removeStaffQuestion
					delStaffAnswer.QuestionnaireAnswerId = questAnswer.Id
					delStaffAnswer.Value = removeStaffAnswerValue
					delStaffAnswer.ChildAnswers = new ArrayList()
					amendStaffAnswer.ChildAnswers.Add(delStaffAnswer)
				end if
				dim ans as Answer= new Answer(cachePolicy)
				ans.Name = removedStaffQuestion
				ans.QuestionnaireAnswerId = questAnswer.Id
				
				if (staffPerson.Person.DegreeList = string.Empty) then
					ans.Value = staffPerson.FullName
				else
					ans.Value = string.Format("{0}, {1}", staffPerson.FullName, staffPerson.Person.DegreeList)
				end if
				
				delStaffAnswer.ChildAnswers.Add(ans)
			else
				'remove from added staff list
				dim addStaffAnswers as ArrayList = questAnswer.GetAnswers(addStaffQuestion)
				dim addStaffAnswer as Answer = nothing
				if (addStaffAnswers.Count > 0) then
					addStaffAnswer = addStaffAnswers(0)
				end if
				if (addStaffAnswer.ChildAnswers is nothing) then
					return
				end if
				
				dim existingValue as string
				if (staffPerson.Person.DegreeList = string.Empty) then
					existingValue = string.Format("{0} -{1}", staffPerson.FullName, staffPerson.Role)
				else
					existingValue = string.Format("{0}, {1} -{2}", staffPerson.FullName, staffPerson.Person.DegreeList, staffPerson.Role)
				end if
				
				for each addedStaffAnswer as Answer in addStaffAnswer.ChildAnswers
					if (addedStaffAnswer.Value = existingValue) then
						addedStaffAnswer.MarkForDelete = true
						exit for
					end if
				next
			end if
		else
			'add only if the person is added by the current process
			if (staffPerson.AddedProtocolProcessId <> currentProcess.Id) then
				return
			end if

			dim addStaffAnswers as ArrayList = questAnswer.GetAnswers(addStaffQuestion)
			dim addStaffAnswer as Answer = nothing
			if (addStaffAnswers.Count > 0) then
				addStaffAnswer = addStaffAnswers(0)
				if (addStaffAnswer.ChildAnswers is nothing) then
					addStaffAnswer.ChildAnswers = new ArrayList()
				end if
			else
				addStaffAnswer = new Answer(cachePolicy)
				addStaffAnswer.Name = addStaffQuestion
				addStaffAnswer.QuestionnaireAnswerId = questAnswer.Id
				addStaffAnswer.Value = addStaffAnswerValue
				addStaffAnswer.ChildAnswers = new ArrayList()
				amendStaffAnswer.ChildAnswers.Add(addStaffAnswer)
			end if

			'check if the added staff is already in the drop down
			dim existingValue as string
				if (staffPerson.Person.DegreeList = string.Empty) then
				existingValue = string.Format("{0} -{1}", staffPerson.FullName, oldRole)
			else
				existingValue = string.Format("{0}, {1} -{2}", staffPerson.FullName, staffPerson.Person.DegreeList, oldRole)
			end if
			
			dim isAdded as boolean = false
			dim ans as Answer = nothing
			for each addedStaffAnswer as Answer in addStaffAnswer.ChildAnswers
				if (addedStaffAnswer.Value = existingValue) then
					isAdded = true
					ans = addedStaffAnswer
					exit for
				end if
			next

			if (not isAdded) then
				ans = new Answer(cachePolicy)
				ans.Name = addedStaffQuestion
				ans.QuestionnaireAnswerId = questAnswer.Id
				if (staffPerson.Person.DegreeList = string.Empty) then
					ans.Value = string.Format("{0} -{1}", staffPerson.FullName, newRole)
				else
					ans.Value = string.Format("{0}, {1} -{2}", staffPerson.FullName, staffPerson.Person.DegreeList, newRole)
				end if
				addStaffAnswer.ChildAnswers.Add(ans)
			else 'update if already existing and oldRole is not equal to newRole
				if (oldRole <> newRole) then
					if (staffPerson.Person.DegreeList = string.Empty) then
						ans.Value = string.Format("{0} -{1}", staffPerson.FullName, newRole)
					else
						ans.Value = string.Format("{0}, {1} -{2}", staffPerson.FullName, staffPerson.Person.DegreeList, newRole)
					end if
				end if
			end if
		end if
		questAnswer.Save(connectionString)
	end sub
end module